package com.huixin.powermonitor.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huixin.powermonitor.common.Result;
import com.huixin.powermonitor.entity.Device;
import com.huixin.powermonitor.entity.RealTimeData;
import com.huixin.powermonitor.service.DeviceService;
import com.huixin.powermonitor.service.RealTimeDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huixin
 * @since 2025-07-08
 */
@CrossOrigin(originPatterns = "*",maxAge = 3600)
@RestController
@RequestMapping("/real-time-data")
public class RealTimeDataController {
    @Autowired
    private RealTimeDataService realTimeDataService;
    @Autowired
    private DeviceService deviceService;
    @Transactional
    @PostMapping("/delete")
    public Result deleteBatch(@RequestBody Long[] id) {
        realTimeDataService.removeByIds(Arrays.asList(id));
        return Result.suc("批量删除成功");
    }
    @PostMapping("/addData")
    public Result add(@RequestBody RealTimeData realTimeData) {
        String real = realTimeData.getDeviceName();
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getDeviceName, real);
        Device device = deviceService.getOne(wrapper);
        realTimeData.setDeviceId(device.getDeviceId());
        realTimeDataService.save(realTimeData);
        return Result.suc("添加成功");
    }
}
